﻿using Microsoft.Extensions.Logging;
using RBAC.MaHaiBo.Entity;
using RBAC.MaHaiBo.Repository.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime;
using System.Text;
using System.Threading.Tasks;

namespace RBAC.MaHaiBo.Service
{
    public class RoleService : IRoleService
    {
        /// <summary>
        /// 日志
        /// </summary>
        ILogger<RoleService> logger;
        /// <summary>
        /// 角色仓储
        /// </summary>
        IRoleRepository rRep;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="logger">日志</param>
        /// <param name="rRep">角色仓储</param>
        public RoleService(ILogger<RoleService> logger,
        IRoleRepository rRep
         )
        {
            this.logger = logger;
            this.rRep = rRep;
        }

        /// <summary>
        /// 添加角色信息
        /// </summary>
        /// <param name="Role">要添加的角色信息</param>
        /// <returns>返回受影响行数</returns>
        public async Task<int> AddRole(RoleInfo role)
        {
            try
            {
                var weiyi = await rRep.GetAsync(p=>p.RName== role.RName&&!p.deleteFlag);
                if (weiyi.Count>0)
                {
                    return -1;
                }
                return await rRep.AddAsync(role);
            }
            catch (Exception ex)
            {
                logger.LogError("【角色服务层】 添加角色信息" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 批量删除角色信息
        /// </summary>
        /// <param name="Ids">要删除的角色编号集合</param>
        /// <returns>返回受影响行数</returns>
        public async Task<int> ByUpdateRole(List<int> Ids)
        {
            try
            {

                var list = rRep.GetAsync(p => Ids.Contains(p.Rid));
                foreach (var item in await list)
                {
                    item.deleteFlag = true;
                }
                return await rRep.ByUpdateAsync(await list);


            }
            catch (Exception ex)
            {
                logger.LogError("【角色服务层】 批量删除角色信息" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 逻辑删除角色编号
        /// </summary>
        /// <param name="uid">角色编号</param>
        /// <returns>返回受影响行数</returns>
        public async Task<int> DeleteRole(int Rid)
        {
            try
            {
                var qauoy = await rRep.GetModelAsync(Rid);
                qauoy.deleteFlag = true;
                return await rRep.UpdateAsync(qauoy);
            }
            catch (Exception ex)
            {
                logger.LogError("【角色服务层】 批量修改角色信息" + ex.Message);
                throw;
            }

        }

        /// <summary>
        /// 显示全部角色信息
        /// </summary>
        /// <returns>返回角色数据集合</returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<FenYeDTO<RoleInfo>> GetRole(int poageiden, int pageSzen, string? Name)
        {
            try
            {
                var fy = new FenYeDTO<RoleInfo>();
                var list = await rRep.GetAllAsync();
                var rqliu = list.Where(p => !p.deleteFlag);
                if (!string.IsNullOrEmpty(Name))
                {
                    rqliu = rqliu.Where(p => p.RName.Contains(Name));
                }

                fy.totyCount = rqliu.Count();
                fy.pageCount = (int)Math.Ceiling(fy.totyCount * 1.0 / pageSzen);
                fy.list = rqliu.OrderBy(p => p.Rid).Skip((poageiden - 1) * pageSzen).Take(pageSzen).ToList();
                return fy;
            }
            catch (Exception ex)
            {
                logger.LogError("【角色服务层】 显示全部角色信息" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 反填角色数据信息
        /// </summary>
        /// <param name="uid">角色编号</param>
        /// <returns>返回数据信息</returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<RoleInfo> GetRoleInfo(int Rid)
        {
            try
            {
                return rRep.GetModelAsync(Rid);

            }
            catch (Exception ex)
            {
                logger.LogError("【角色服务层】 反填角色数据信息" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 修改角色信息
        /// </summary>
        /// <param name="Role">修改后的数据信息</param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<int> UpdateRole(RoleInfo Role)
        {
            try
            {
                var weiyi = await rRep.GetAsync(p=>p.Rid!=Role.Rid&&!p.deleteFlag&&p.RName== Role.RName);
                if (weiyi.Count>0) 
                {
                    return -1; 
                }
                Role.modifier = "李四";
                Role.modificationTime = DateTime.Now;
                return await rRep.UpdateAsync(Role);

            }
            catch (Exception ex)
            {
                logger.LogError("【角色服务层】 批量修改角色信息" + ex.Message);
                throw;
            }
        }

    }
}
